package com.jay.lang.clazz.jsr199;

import java.util.Arrays;
import java.util.List;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class Test15 {
	public static void main(String[] args) {
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // Line 1.
		DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
		StandardJavaFileManager fileManager = compiler.getStandardFileManager(
				diagnosticsCollector, null, null); // Line 3.
		String fileToCompile = "D:/temp/aaa.java"; // Line
																			// 4
		Iterable<? extends JavaFileObject> fileObjects = fileManager
				.getJavaFileObjectsFromStrings(Arrays.asList(fileToCompile)); // Line
																				// 5
		CompilationTask task = compiler.getTask(null, fileManager,
				diagnosticsCollector, null, null, fileObjects); // Line 6
		Boolean result = task.call(); // Line 7
		List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticsCollector
				.getDiagnostics();
		for (Diagnostic<? extends JavaFileObject> d : diagnostics) {
			// Print all the information here.
			System.out.println(d.getMessage(null));
		}
		if (result == true) {
			System.out.println("Compilation has succeeded");
		} else {
			System.out.println("Compilation fails.");
		}
	}

}
